package org.example.exception;


import lombok.extern.slf4j.Slf4j;
import org.example.vo.ResultVo;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 处理自定义业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public ResultVo<Void> handleBusinessException(BusinessException e, HttpServletRequest request) {
        // 记录异常日志（包含请求路径）
        log.error("业务异常：{}，请求路径：{}", e.getMessage(), request.getRequestURI());
        // 返回自定义错误信息
        ResultVo<Void> errorVo = new ResultVo<>();
        errorVo.error(e.getCode(), e.getMessage());
        return errorVo;
    }
}
